package com.qf.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.qf.pojo.DtsKeyword;
import com.qf.service.DtsKeywordService;
import com.qf.util.ResponseUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * projectName:workspace_6
 * author:hyn
 * time:2021/12/5 17:16
 * description:商品管理 关键词
 */
@CrossOrigin("*")
@RestController
@RequestMapping("/admin/keyword")
public class AdminKeyWordController {
    @Autowired
    private DtsKeywordService keywordService;
    //关键词列表
    @RequestMapping("list")
    public Object list(String keyword, String url,
                       @RequestParam(name = "page",required = false,defaultValue = "1") Integer page,
                       @RequestParam(name = "limit",required = false,defaultValue = "20") Integer limit,
                       @RequestParam(name = "sort",required = false,defaultValue = "add_time") String sort,
                       @RequestParam(name = "order",required = false,defaultValue = "desc") String order
    ){
        IPage<DtsKeyword> articleList = keywordService.findKeyWordAllList(keyword,url,page,limit,sort,order);

        Map<String, Object> data = new HashMap<>();
        data.put("total",articleList.getTotal());
        data.put("items",articleList.getRecords());
        return ResponseUtil.ok(data);
    }
    //关键词添加
    @RequestMapping("create")
    public Object create(@RequestBody DtsKeyword keyword){
        if(keyword == null){
            return ResponseUtil.badArgument();
        }
        return keywordService.insert(keyword);
    }
    //关键词修改
    @RequestMapping("update")
    public Object update(@RequestBody DtsKeyword keyword){
        if(keyword == null){
            return ResponseUtil.badArgument();
        }
        return keywordService.update(keyword);
    }
    //关键词删除
    @RequestMapping("delete")
    public Object delete(@RequestBody DtsKeyword keyword){
        if(keyword == null){
            return ResponseUtil.badArgument();
        }
        return keywordService.delete(keyword);
    }
}
